/**
 * 功能:
 * 作者:周逸健
 * 日期: 2023/11/1 15:24
 */
package com.cgnfz.crm.controller;

import com.cgnfz.crm.entity.RestBean;
import com.cgnfz.crm.entity.vo.request.CustomerVO;
import com.cgnfz.crm.entity.vo.response.CustomerResp;
import com.cgnfz.crm.service.CustomerService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.function.Supplier;

@Validated
@RestController
@RequestMapping("/api/customer")
public class CustomerController {

    Logger log = LoggerFactory.getLogger(CustomerController.class);

    @Resource
    CustomerService customerService;

    @GetMapping("/selectPage")
    public RestBean<CustomerResp> selectPage(@RequestParam String employeeId,
                                             @RequestParam String customer,
                                             @RequestParam Long pageNum, @RequestParam Long pageSize) {
        return RestBean.success(customerService.selectPage(employeeId, customer, pageNum, pageSize));
    }


    @PostMapping("/insert")
    public RestBean<Void> insert(@RequestBody CustomerVO customerVO) {
        return this.messageHandle(() -> customerService.insert(customerVO));
    }

    @PostMapping("/updateByStatus")
    public RestBean<Void> updateByStatus(@RequestBody CustomerVO customerVO) {
        return this.messageHandle(() -> customerService.updateByStatus(customerVO));
    }


    @GetMapping("/myCustomer")
    public RestBean<CustomerResp> myCustomer(@RequestParam String employeeId) {
        return RestBean.success(customerService.myCustomer(employeeId));
    }

    @GetMapping("/approveList")
    public RestBean<CustomerResp> approveList(@RequestParam String customer){
        return RestBean.success(customerService.approveList(customer));
    }


    /**
     * 针对于返回值为String作为错误信息的方法进行统一处理
     *
     * @param action 具体操作
     * @param <T>    响应结果类型
     * @return 响应结果
     */
    private <T> RestBean<T> messageHandle(Supplier<String> action) {
        String message = action.get();
        if (message == null)
            return RestBean.success();
        else
            return RestBean.failure(400, message);
    }

}
